/////precompiled////////
#include "precompiled.h"
////////////////////////


#include "frm_control_TScrollBar.h"
using namespace frm;
using namespace control;

// TScrollBarVertBegButton ////////////////////////////////////////////////////

//
void TScrollBarVertBegButton::TStyle::SetDefault()
{
  filler::TRoundRect
    Filler1(grf::TColor(255,255,255),grf::TColor(  0,  0,123)),
    Filler2(grf::TColor(255,255,255),grf::TColor(255,255,  0)),
    Filler3(grf::TColor(255,255,255),grf::TColor(  0,  0,255)),
    Filler4(grf::TColor(255,255,255),grf::TColor(  0,  0,  0)),
    Filler5(grf::TColor(255,255,255),grf::TColor(  0,  0,123)),
    Filler6(grf::TColor(255,255,255),grf::TColor(255,  0,  0));

  Normal.Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  Normal.Filler.Set(Filler3);
  Down  .Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  Down  .Filler.Set(Filler4);
  Flat  .Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  Flat  .Filler.Set(Filler6);
}
  
//
void TScrollBarVertBegButton::TComponent_InheritStyleFrom(TTableOfStyles &ParentStyles)
{ 
  TStyle *Style = dynamic_cast<TStyle*>(ParentStyles.SearchByEqualType(TStyle::TType())); if (Style) {
    SetStyle(*Style);
  }
  else {
    //TButton::TComponent_InheritStyleFrom(ParentStyles);
  }
}

// TScrollBarHorzBegButton ////////////////////////////////////////////////////

//
void TScrollBarHorzBegButton::TStyle::SetDefault()
{
  filler::TRoundRect
    Filler1(grf::TColor(255,255,255),grf::TColor(  0,  0,123)),
    Filler2(grf::TColor(255,255,255),grf::TColor(255,255,  0)),
    Filler3(grf::TColor(255,255,255),grf::TColor(  0,  0,255)),
    Filler4(grf::TColor(255,255,255),grf::TColor(  0,  0,  0)),
    Filler5(grf::TColor(255,255,255),grf::TColor(  0,  0,123)),
    Filler6(grf::TColor(255,255,255),grf::TColor(255,  0,  0));

  Normal.Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  Normal.Filler.Set(Filler3);
  Down  .Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  Down  .Filler.Set(Filler2);
  Flat  .Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  Flat  .Filler.Set(Filler6);
}

//
void TScrollBarHorzBegButton::TComponent_InheritStyleFrom(TTableOfStyles &ParentStyles)
{ 
  TStyle *Style = dynamic_cast<TStyle*>(ParentStyles.SearchByEqualType(TStyle::TType())); if (Style) {
    SetStyle(*Style);
  }
  else {
    //TButton::TComponent_InheritStyleFrom(ParentStyles);
  }
}

// TScrollBarVertBegPage //////////////////////////////////////////////////////

//
void TScrollBarVertBegPage::TStyle::SetDefault()
{
  filler::TRoundRect
    Filler1(grf::TColor(255,255,255),grf::TColor(  0,  0,123)),
    Filler2(grf::TColor(255,255,255),grf::TColor(255,255,  0)),
    Filler3(grf::TColor(255,255,255),grf::TColor(  0,  0,255)),
    Filler4(grf::TColor(255,255,255),grf::TColor(  0,  0,  0)),
    Filler5(grf::TColor(255,255,255),grf::TColor(  0,  0,123)),
    Filler6(grf::TColor(255,255,255),grf::TColor(255,  0,  0));

  Normal.Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  Normal.Filler.Set(Filler3);
  Down  .Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  Down  .Filler.Set(Filler2);
  Flat  .Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  Flat  .Filler.Set(Filler6);
}

//
void TScrollBarVertBegPage::TComponent_InheritStyleFrom(TTableOfStyles &ParentStyles)
{ 
  TStyle *Style = dynamic_cast<TStyle*>(ParentStyles.SearchByEqualType(TStyle::TType())); if (Style) {
    SetStyle(*Style);
  }
  else {
    //TButton::TComponent_InheritStyleFrom(ParentStyles);
  }
}

// TScrollBarHorzBegPage //////////////////////////////////////////////////////

//
void TScrollBarHorzBegPage::TStyle::SetDefault()
{
  filler::TRoundRect
    Filler1(grf::TColor(255,255,255),grf::TColor(  0,  0,123)),
    Filler2(grf::TColor(255,255,255),grf::TColor(255,255,  0)),
    Filler3(grf::TColor(255,255,255),grf::TColor(  0,  0,255)),
    Filler4(grf::TColor(255,255,255),grf::TColor(  0,  0,  0)),
    Filler5(grf::TColor(255,255,255),grf::TColor(  0,  0,123)),
    Filler6(grf::TColor(255,255,255),grf::TColor(255,  0,  0));

  Normal.Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  Normal.Filler.Set(Filler3);
  Down  .Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  Down  .Filler.Set(Filler2);
  Flat  .Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  Flat  .Filler.Set(Filler6);
}

//
void TScrollBarHorzBegPage::TComponent_InheritStyleFrom(TTableOfStyles &ParentStyles)
{ 
  TStyle *Style = dynamic_cast<TStyle*>(ParentStyles.SearchByEqualType(TStyle::TType())); if (Style) {
    SetStyle(*Style);
  }
  else {
    //TButton::TComponent_InheritStyleFrom(ParentStyles);
  }
}

// TScrollBarVertTrack ////////////////////////////////////////////////////////

//
void TScrollBarVertTrack::TStyle::SetDefault()
{
  filler::TRoundRect
    Filler1(grf::TColor(255,255,255),grf::TColor(  0,  0,123)),
    Filler2(grf::TColor(255,255,255),grf::TColor(255,255,  0)),
    Filler3(grf::TColor(255,255,255),grf::TColor(  0,  0,255)),
    Filler4(grf::TColor(255,255,255),grf::TColor(  0,  0,  0)),
    Filler5(grf::TColor(255,255,255),grf::TColor(  0,  0,123)),
    Filler6(grf::TColor(255,255,255),grf::TColor(255,  0,  0));

  Normal.Cursor.Set(cursor::TCursorWin(IDC_SIZENS));
  Normal.Filler.Set(Filler5);
  Down  .Cursor.Set(cursor::TCursorWin(IDC_SIZENS));
  Down  .Filler.Set(Filler2);
}

//
void TScrollBarVertTrack::TComponent_InheritStyleFrom(TTableOfStyles &ParentStyles)
{ 
  TStyle *Style = dynamic_cast<TStyle*>(ParentStyles.SearchByEqualType(TStyle::TType())); if (Style) {
    SetStyle(*Style);
  }
  else {
    //TSplitter::TComponent_InheritStyleFrom(ParentStyles);
  }
}

// TScrollBarHorzTrack //////////////////////////////////////////////

//
void TScrollBarHorzTrack::TStyle::SetDefault()
{
  filler::TRoundRect
    Filler1(grf::TColor(255,255,255),grf::TColor(  0,  0,123)),
    Filler2(grf::TColor(255,255,255),grf::TColor(255,255,  0)),
    Filler3(grf::TColor(255,255,255),grf::TColor(  0,  0,255)),
    Filler4(grf::TColor(255,255,255),grf::TColor(  0,  0,  0)),
    Filler5(grf::TColor(255,255,255),grf::TColor(  0,  0,123)),
    Filler6(grf::TColor(255,255,255),grf::TColor(255,  0,  0));

  Normal.Cursor.Set(cursor::TCursorWin(IDC_SIZEWE));
  Normal.Filler.Set(Filler5);
  Down  .Cursor.Set(cursor::TCursorWin(IDC_SIZEWE));
  Down  .Filler.Set(Filler2);
}

//
void TScrollBarHorzTrack::TComponent_InheritStyleFrom(TTableOfStyles &ParentStyles)
{ 
  TStyle *Style = dynamic_cast<TStyle*>(ParentStyles.SearchByEqualType(TStyle::TType())); if (Style) {
    SetStyle(*Style);
  }
  else {
    //TSplitter::TComponent_InheritStyleFrom(ParentStyles);
  }
}

// TScrollBarVertEndPage //////////////////////////////////////////////////////

//
void TScrollBarVertEndPage::TStyle::SetDefault()
{
  filler::TRoundRect
    Filler1(grf::TColor(255,255,255),grf::TColor(  0,  0,123)),
    Filler2(grf::TColor(255,255,255),grf::TColor(255,255,  0)),
    Filler3(grf::TColor(255,255,255),grf::TColor(  0,  0,255)),
    Filler4(grf::TColor(255,255,255),grf::TColor(  0,  0,  0)),
    Filler5(grf::TColor(255,255,255),grf::TColor(  0,  0,123)),
    Filler6(grf::TColor(255,255,255),grf::TColor(255,  0,  0));

  Normal.Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  Normal.Filler.Set(Filler3);
  Down  .Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  Down  .Filler.Set(Filler2);
  Flat  .Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  Flat  .Filler.Set(Filler6);
}

//
void TScrollBarVertEndPage::TComponent_InheritStyleFrom(TTableOfStyles &ParentStyles)
{ 
  TStyle *Style = dynamic_cast<TStyle*>(ParentStyles.SearchByEqualType(TStyle::TType())); if (Style) {
    SetStyle(*Style);
  }
  else {
    //TButton::TComponent_InheritStyleFrom(ParentStyles);
  }
}

// TScrollBarHorzEndPage //////////////////////////////////////////////////////

//
void TScrollBarHorzEndPage::TStyle::SetDefault()
{
  filler::TRoundRect
    Filler1(grf::TColor(255,255,255),grf::TColor(  0,  0,123)),
    Filler2(grf::TColor(255,255,255),grf::TColor(255,255,  0)),
    Filler3(grf::TColor(255,255,255),grf::TColor(  0,  0,255)),
    Filler4(grf::TColor(255,255,255),grf::TColor(  0,  0,  0)),
    Filler5(grf::TColor(255,255,255),grf::TColor(  0,  0,123)),
    Filler6(grf::TColor(255,255,255),grf::TColor(255,  0,  0));

  Normal.Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  Normal.Filler.Set(Filler3);
  Down  .Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  Down  .Filler.Set(Filler2);
  Flat  .Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  Flat  .Filler.Set(Filler6);
}

//
void TScrollBarHorzEndPage::TComponent_InheritStyleFrom(TTableOfStyles &ParentStyles)
{ 
  TStyle *Style = dynamic_cast<TStyle*>(ParentStyles.SearchByEqualType(TStyle::TType())); if (Style) {
    SetStyle(*Style);
  }
  else {
    //TButton::TComponent_InheritStyleFrom(ParentStyles);
  }
}

// TScrollBarVertEndButton ////////////////////////////////////////////////////

//
void TScrollBarVertEndButton::TStyle::SetDefault()
{
  filler::TRoundRect
    Filler1(grf::TColor(255,255,255),grf::TColor(  0,  0,123)),
    Filler2(grf::TColor(255,255,255),grf::TColor(255,255,  0)),
    Filler3(grf::TColor(255,255,255),grf::TColor(  0,  0,255)),
    Filler4(grf::TColor(255,255,255),grf::TColor(  0,  0,  0)),
    Filler5(grf::TColor(255,255,255),grf::TColor(  0,  0,123)),
    Filler6(grf::TColor(255,255,255),grf::TColor(255,  0,  0));

  Normal.Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  Normal.Filler.Set(Filler3);
  Down  .Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  Down  .Filler.Set(Filler2);
  Flat  .Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  Flat  .Filler.Set(Filler6);
}

//
void TScrollBarVertEndButton::TComponent_InheritStyleFrom(TTableOfStyles &ParentStyles)
{ 
  TStyle *Style = dynamic_cast<TStyle*>(ParentStyles.SearchByEqualType(TStyle::TType())); if (Style) {
    SetStyle(*Style);
  }
  else {
    //TButton::TComponent_InheritStyleFrom(ParentStyles);
  }
}

// TScrollBarHorzEndButton ////////////////////////////////////////////////////

//
void TScrollBarHorzEndButton::TStyle::SetDefault()
{
  filler::TRoundRect
    Filler1(grf::TColor(255,255,255),grf::TColor(  0,  0,123)),
    Filler2(grf::TColor(255,255,255),grf::TColor(255,255,  0)),
    Filler3(grf::TColor(255,255,255),grf::TColor(  0,  0,255)),
    Filler4(grf::TColor(255,255,255),grf::TColor(  0,  0,  0)),
    Filler5(grf::TColor(255,255,255),grf::TColor(  0,  0,123)),
    Filler6(grf::TColor(255,255,255),grf::TColor(255,  0,  0));

  Normal.Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  Normal.Filler.Set(Filler3);
  Down  .Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  Down  .Filler.Set(Filler2);
  Flat  .Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  Flat  .Filler.Set(Filler6);
}

//
void TScrollBarHorzEndButton::TComponent_InheritStyleFrom(TTableOfStyles &ParentStyles)
{ 
  TStyle *Style = dynamic_cast<TStyle*>(ParentStyles.SearchByEqualType(TStyle::TType())); if (Style) {
    SetStyle(*Style);
  }
  else {
    //TButton::TComponent_InheritStyleFrom(ParentStyles);
  }
}

// TScrollBar /////////////////////////////////////////////////////////////////

//
void TScrollBar::TStyle::SetFromParent(frm::TStyle *Parent_)
{
  if (!Parent_) {
    return;
  }

  TStyle *Parent = dynamic_cast<TStyle*>(Parent_);
  
  TPanel::TStyle::SetFromParent(Parent);
  
  BegButton.SetFromParent(&Parent->BegButton);
  BegPage  .SetFromParent(&Parent->BegPage);
  track    .SetFromParent(&Parent->track);
  EndButton.SetFromParent(&Parent->EndButton);
  EndPage  .SetFromParent(&Parent->EndPage);
  Weight = Parent->Weight;
}

//
svl::TInfoParam TScrollBar::TStyle::AddInfoTo(svl::TInfoParam Info)
{
  return TPanel::TStyle::AddInfoTo(Info)
   .Add("BegButton",BegButton)
   .Add("BegPage"  ,BegPage)
   .Add("track"    ,track)
   .Add("EndButton",EndButton)
   .Add("EndPage"  ,EndPage)
   .Add("Weight"   ,Weight)
  ;
}

//
void TScrollBar::TStyleVert::SetDefault()
{
  filler::TRoundRect
    Filler1(grf::TColor(255,255,255),grf::TColor(  0,  0,123)),
    Filler2(grf::TColor(255,255,255),grf::TColor(255,255,  0)),
    Filler3(grf::TColor(255,255,255),grf::TColor(  0,  0,255)),
    Filler4(grf::TColor(255,255,255),grf::TColor(  0,  0,  0)),
    Filler5(grf::TColor(255,255,255),grf::TColor(  0,  0,123)),
    Filler6(grf::TColor(255,255,255),grf::TColor(255,  0,  0));

  BegButton.Normal.Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  BegButton.Normal.Filler.Set(Filler3);
  BegButton.Down  .Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  BegButton.Down  .Filler.Set(Filler4);
  BegButton.Flat  .Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  BegButton.Flat  .Filler.Set(Filler6);
  //
  BegPage.Normal.Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  BegPage.Normal.Filler.Set(Filler3);
  BegPage.Down  .Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  BegPage.Down  .Filler.Set(Filler2);
  BegPage.Flat  .Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  BegPage.Flat  .Filler.Set(Filler6);
  //
  Track.Normal.Cursor.Set(cursor::TCursorWin(IDC_SIZENS));
  Track.Normal.Filler.Set(Filler5);
  Track.Down  .Cursor.Set(cursor::TCursorWin(IDC_SIZENS));
  Track.Down  .Filler.Set(Filler2);
  //
  EndPage.Normal.Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  EndPage.Normal.Filler.Set(Filler3);
  EndPage.Down  .Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  EndPage.Down  .Filler.Set(Filler2);
  EndPage.Flat  .Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  EndPage.Flat  .Filler.Set(Filler6);
  //
  EndButton.Normal.Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  EndButton.Normal.Filler.Set(Filler3);
  EndButton.Down  .Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  EndButton.Down  .Filler.Set(Filler2);
  EndButton.Flat  .Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  EndButton.Flat  .Filler.Set(Filler6);

  Weight = 15;
}

//
void TScrollBar::TStyleHorz::SetDefault()
{
  filler::TRoundRect
    Filler1(grf::TColor(255,255,255),grf::TColor(  0,  0,123)),
    Filler2(grf::TColor(255,255,255),grf::TColor(255,255,  0)),
    Filler3(grf::TColor(255,255,255),grf::TColor(  0,  0,255)),
    Filler4(grf::TColor(255,255,255),grf::TColor(  0,  0,  0)),
    Filler5(grf::TColor(255,255,255),grf::TColor(  0,  0,123)),
    Filler6(grf::TColor(255,255,255),grf::TColor(255,  0,  0));

  BegButton.Normal.Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  BegButton.Normal.Filler.Set(Filler3);
  BegButton.Down  .Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  BegButton.Down  .Filler.Set(Filler2);
  BegButton.Flat  .Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  BegButton.Flat  .Filler.Set(Filler6);
  //
  BegPage.Normal.Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  BegPage.Normal.Filler.Set(Filler3);
  BegPage.Down  .Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  BegPage.Down  .Filler.Set(Filler2);
  BegPage.Flat  .Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  BegPage.Flat  .Filler.Set(Filler6);
  //
  Track.Normal.Cursor.Set(cursor::TCursorWin(IDC_SIZEWE));
  Track.Normal.Filler.Set(Filler5);
  Track.Down  .Cursor.Set(cursor::TCursorWin(IDC_SIZEWE));
  Track.Down  .Filler.Set(Filler2);
  //
  EndPage.Normal.Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  EndPage.Normal.Filler.Set(Filler3);
  EndPage.Down  .Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  EndPage.Down  .Filler.Set(Filler2);
  EndPage.Flat  .Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  EndPage.Flat  .Filler.Set(Filler6);
  //
  EndButton.Normal.Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  EndButton.Normal.Filler.Set(Filler3);
  EndButton.Down  .Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  EndButton.Down  .Filler.Set(Filler2);
  EndButton.Flat  .Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  EndButton.Flat  .Filler.Set(Filler6);

  Weight = 15;
}

//
TScrollBar::TScrollBar(TWindow &Window,EScroll Scroll) : TPanel(Window)
,resizeScroll(*this),resizeBegPage(*this)
,evButton(*this),evPage(*this)
{
  scroll = Scroll; increment = range = position = 0;
  SetIncrement(); SetRange(); SetPosition();
  TTempDisableCalcSize D(this);

  switch (scroll)
  {
    case Vert:
    {
      //Rect.X.Set(0                    ,EDirectChildren_All,EAlignChildren_Begin);
      //Rect.Y.Set(ECalcSize_SiblingsMax,EDirectChildren_Seq,EAlignChildren_Begin);
      Width_Fixed(0).Height_SiblingsMax().DirectChildren_Vert()
       .WAlignChildren_Begin().HAlignChildren_Begin();
      GetHOrdinate().SetEvents(resizeScroll);

      BegButton = new TScrollBarVertBegButton(Window); AddChildLast(BegButton);
      BegButton->Width_Parent().Height_Fixed(20);

      BegPage = new TScrollBarVertBegPage(Window); AddChildLast(BegPage);
      BegPage->GetHOrdinate().SetEvents(resizeBegPage);
      BegPage->Width_Parent();

      Track = new TScrollBarVertTrack(Window,BegPage); AddChildLast(Track);
      Track->Width_Parent();
      Track->SetMinSize(0);

      EndPage = new TScrollBarVertEndPage(Window); AddChildLast(EndPage);
      EndPage->Width_Parent().Height_SiblingsRem();

      EndButton = new TScrollBarVertEndButton(Window); AddChildLast(EndButton);
      EndButton->Width_Parent().Height_Fixed(20);

      BegPage->SetHeight(TrackPositionFromPosition(position));
    }
    break;

    case Horz:
    {
      //Rect.X.Set(ECalcSize_SiblingsMax,EDirectChildren_Seq,EAlignChildren_Begin);
      //Rect.Y.Set(0                    ,EDirectChildren_All,EAlignChildren_Begin);
      Width_SiblingsMax().Height_Fixed(0).DirectChildren_Horz()
       .WAlignChildren_Begin().HAlignChildren_Begin();
      GetWOrdinate().SetEvents(resizeScroll);

      BegButton = new TScrollBarHorzBegButton(Window); AddChildLast(BegButton);
      BegButton->Width_Fixed(20).Height_Parent();

      BegPage = new TScrollBarHorzBegPage(Window); AddChildLast(BegPage);
      BegPage->GetWOrdinate().SetEvents(resizeBegPage);
      BegPage->Height_Parent();

      Track = new TScrollBarHorzTrack(Window,BegPage); AddChildLast(Track);
      //Splitter->Rect.X.SetSize(10);
      Track->Height_Parent();
      Track->SetMinSize(0);

      EndPage = new TScrollBarHorzEndPage(Window); AddChildLast(EndPage);
      EndPage->Width_SiblingsRem().Height_Parent();

      EndButton = new TScrollBarHorzEndButton(Window); AddChildLast(EndButton);
      EndButton->Width_Fixed(20).Height_Parent();

      BegPage->SetWidth(TrackPositionFromPosition(position));
    }
    break;
  }

  SetSize();

  BegButton->Events.Link(evButton);
  BegPage  ->Events.Link(evPage);
  EndPage  ->Events.Link(evPage);
  EndButton->Events.Link(evButton);
  
  static TStyles Styles; SetStyle(Styles.Get(Scroll));
}

//
TScrollBar::~TScrollBar()
{
}

//
TScrollBar &TScrollBar::SetStyle(TStyle &V)
{
  if (scroll != V.GetEScroll()) { 
    Except("SetStyle[scroll != V.GetEScroll()]");
  } 
  //style = &V; 
  TPanel::SetStyle(V);

  //switch (scroll)
  //{
  //  case Vert: Rect.X.SetSize(V.Place[scroll].Size.Get()); break;
  //  case Horz: Rect.Y.SetSize(V.Place[scroll].Size.Get()); break;
  //}

  BegButton->SetStyle(V.BegButton);
  BegPage  ->SetStyle(V.BegPage);
  Track    ->SetStyle(V.track);
  EndPage  ->SetStyle(V.EndPage);
  EndButton->SetStyle(V.EndButton);
  SetSize( V.Weight );
  
  return *this;
}

//
int TScrollBar::GetSize()
{
  switch (scroll) {
    case Vert: return GetWidth(); //GetWidthCalculated();//Rect.X.GetCalculatedSize();
    case Horz: return GetHeight(); //GetHeightCalculated();//Rect.Y.GetCalculatedSize();
  }
  return 0;
}

//
void TScrollBar::SetSize(int V)
{
  switch (scroll) {
    case Vert: return SetWidth (V);
    case Horz: return SetHeight(V);
  }
}

//
void TScrollBar::SetRange(int V)
{
  range = V;
}

//
void TScrollBar::UpdateRange(int V)
{
  if (range == V) { 
    return;
  }
  SetRange(V); 
  UpdateSplitterSize();
}

//
void TScrollBar::SetPosition(int V)
{
  V = max(0,V); 
  V = min(range,V);

  int OldPosition = position; 
  position = V;

  Events.Get().TEvents_AfterChangePosition(
    TEvents::TI(Events),*this,OldPosition,position
  );

  //////////////////////////////
  int PageSize = CalcPageSize(); 
  
  if (V > range - PageSize) { 
    V = range - PageSize; 
    V = max(0,V); 
  }
  if (OldPosition != V) { 
    Events.Get().TEvents_AfterScroll(TEvents::TI(Events),*this,V); 
  }
}

//
void TScrollBar::UpdatePosition(int V)
{
  int PageSize = CalcPageSize();
  //V = max(0,V); V = min(range,V);
  V = min(range-PageSize,V); V = max(0,V); 
  if (position == V) { 
    return;
  }

  geometry::EOrdinate Ordinate; 
  
  switch (scroll) {
    case Vert: Ordinate = GetHOrdinate().GetEOrdinate(); break;
    case Horz: Ordinate = GetWOrdinate().GetEOrdinate(); break;
  }
  SetPosition(V);

/*
  if
  (
    BegPage->Node.Geometry.Size[Ordinate]->GetSize() == 0 &&
    EndPage->Node.Geometry.Size[Ordinate]->GetSize() == 0
  )
  { return;}
*/

  svl::TPStack PStack(this);
  BegPage->GetOrdinate(Ordinate).UpdateSize(
    TrackPositionFromPosition(V)
  );
}

//
void TScrollBar::UpdateSplitterSize()
{
  BeginPaint();
  svl::TPStack PStack(this);

  geometry::EOrdinate Ordinate; 
  
  switch (scroll) {
    case Vert: Ordinate = GetHOrdinate().GetEOrdinate(); break;
    case Horz: Ordinate = GetWOrdinate().GetEOrdinate(); break;
  }

  int SplitterSize = 0;
  int SBSize       = GetOrdinate(Ordinate).GetClientSize();
  int ButtonsSize  = BegButton->GetOrdinate(Ordinate).GetSize()
                   + EndButton->GetOrdinate(Ordinate).GetSize();

  int    BegPageSize = BegPage->GetOrdinate(Ordinate).GetSize();
  double TrackSize   = SBSize - ButtonsSize;

  double PageCount = 0; 
  
  if (SBSize > 0) { 
    PageCount = (double)range / (double)SBSize; 
  }
  
  SplitterSize = 0; 
  
  if (PageCount > 0) {
    SplitterSize = (int)min(TrackSize,TrackSize/PageCount); 
  }

  if (BegPageSize + SplitterSize > TrackSize) { 
    BegPage->GetOrdinate(Ordinate).UpdateSize( (int)(TrackSize - SplitterSize) ); 
  }
  Track->GetOrdinate(Ordinate).UpdateSize(SplitterSize);

  // Correct ///////////////////
  int PageSize = CalcPageSize();

  if (position > range - PageSize) {
    int NewPosition = range - PageSize;
    NewPosition = max(0,NewPosition);
    UpdatePosition(NewPosition);
  }
  else {
    int TrackPosition    = BegPage->GetOrdinate(Ordinate).GetSize();
    int NewTrackPosition = TrackPositionFromPosition(position);

    if (TrackPosition != NewTrackPosition) { 
      BegPage->GetOrdinate(Ordinate).UpdateSize(NewTrackPosition); 
    }
  }
  EndPaint();
}

//
int TScrollBar::PositionFromTrackPosition(int TrackPosition)
{
  geometry::EOrdinate Ordinate; 
  
  switch (scroll) {
    case Vert: Ordinate = GetHOrdinate().GetEOrdinate(); break;
    case Horz: Ordinate = GetWOrdinate().GetEOrdinate(); break;
  }

  int SBSize      = GetOrdinate(Ordinate).GetClientSize();
  int ButtonsSize = BegButton->GetOrdinate(Ordinate).GetSize()
                  + EndButton->GetOrdinate(Ordinate).GetSize();
  int TrackSize   = SBSize - ButtonsSize;

  return (int)( (double)TrackPosition / (double)(TrackSize) * (double)range );
}

//
int TScrollBar::TrackPositionFromPosition(int Position)
{
  if (range == 0) { return 0;}
  
  geometry::EOrdinate Ordinate; 
  
  switch (scroll) {
    case Vert: Ordinate = GetHOrdinate().GetEOrdinate(); break;
    case Horz: Ordinate = GetWOrdinate().GetEOrdinate(); break;
  }

  int SBSize      = GetOrdinate(Ordinate).GetClientSize();
  int ButtonsSize = BegButton->GetOrdinate(Ordinate).GetSize()
                  + EndButton->GetOrdinate(Ordinate).GetSize();
  int TrackSize   = SBSize - ButtonsSize;

  return (int)( (double)Position / (double)range * (double)(TrackSize) );
}

//
int TScrollBar::CalcPageSize()
{
  geometry::EOrdinate Ordinate; 
  
  switch (scroll) {
    case Vert: Ordinate = GetHOrdinate().GetEOrdinate(); break;
    case Horz: Ordinate = GetWOrdinate().GetEOrdinate(); break;
  }

  int SBSize   = GetOrdinate(Ordinate).GetClientSize();
  int PageSize = range;//0;

  if (SBSize > 0) {
    double PageCount = (double)range / (double)SBSize; 
    
    if (PageCount > 0) {
      PageSize = (int)( (double)range / PageCount );
    }
  }
  return PageSize;
}

//
void TScrollBar::TComponent_InheritStyleFrom(TTableOfStyles &ParentStyles)
{ 
  TStyle *Style = dynamic_cast<TStyle*>(ParentStyles.SearchByEqualType(TStyle::TType())); if (Style) {
    SetStyle(*Style);
  }
  else {
    //TPanel::TComponent_InheritStyleFrom(ParentStyles);
  }
}

//
void TScrollBar::TResizeScroll::TEvents_AfterChangeSize(geometry::TOrdinate &Sender,int OldVal,int NewVal)
{
  sb.UpdateSplitterSize();
}

//
void TScrollBar::TResizeBegPage::TEvents_BeforeChangeSize(geometry::TOrdinate &Sender,int OldVal,int &NewVal)
{
  NewVal = max(0,NewVal);
  geometry::EOrdinate Ordinate = Sender.GetEOrdinate();

  int SBSize    = sb.GetOrdinate(Ordinate).GetClientSize();
  int TrackSize = sb.BegButton->GetOrdinate(Ordinate).GetSize()
                + sb.Track    ->GetOrdinate(Ordinate).GetSize()
                + sb.EndButton->GetOrdinate(Ordinate).GetSize();

  if (TrackSize + NewVal > SBSize) {
    NewVal = max(0,SBSize - TrackSize);
  }

  if (!svl::TPStack::IsTop(&sb)) {
    int PageSize    = sb.CalcPageSize();
    int NewPosition = sb.PositionFromTrackPosition(NewVal);

    if (NewPosition > sb.range - PageSize) {
      NewPosition = sb.range - PageSize;
      NewPosition = max(0,NewPosition);
    }
    sb.SetPosition(NewPosition);
  }
}

//
void TScrollBar::TEvButton::TEvents_AfterDown(TI &I,TButton &Sender)
{
  TButton::TEvents::TEvents_AfterDown(I,Sender);

  int Inc,PageSize = sb.CalcPageSize();

  if (sb.BegButton == &Sender) { 
    Inc = -sb.increment;
  }
  else if (sb.EndButton == &Sender) { 
    Inc = +sb.increment;
  }

  int NewPosition = max(0,sb.position + Inc);

  if (NewPosition > sb.range - PageSize) {
    NewPosition = sb.range - PageSize;
    NewPosition = max(0,NewPosition);
  }
  sb.UpdatePosition(NewPosition);
}

//
void TScrollBar::TEvPage::TEvents_AfterDown(TI&I,TButton &Sender)
{
  TButton::TEvents::TEvents_AfterDown(I,Sender);

  int Inc,PageSize = sb.CalcPageSize();

  if (sb.BegPage == &Sender) { 
    Inc = -PageSize;
  }
  else if (sb.EndPage == &Sender) { 
    Inc = +PageSize;
  }

  int NewPosition = max(0,sb.position + Inc);

  if (NewPosition > sb.range - PageSize) {
    NewPosition = sb.range - PageSize;
    NewPosition = max(0,NewPosition);
  }
  sb.UpdatePosition(NewPosition);
}
